﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevToolBox.Cloud.Common;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure;
using System.Data;

namespace DevToolBox.Cloud.Azure
{

    public class TableStorageTransactions: ITableCommand
    {
        private CloudTableClient tableClient = null;

        public TableStorageTransactions()
        {
            CloudStorageAccount storageAccount = CloudStorageAccount
                                                    .Parse(CloudConfigurationManager
                                                    .GetSetting("AzureStorageConnectionString"));            
            tableClient = storageAccount.CreateCloudTableClient();
        }

        //public IEnumerable<T> Query<T>(object key, string conditionOperator, object[] values)
        //{
        //    CloudTable table = tableClient.GetTableReference("people");
        //    string tableOperator = string.Empty;

        //    //BW: The operator Begins with is not supported
        //    switch (conditionOperator)
        //    {
        //        case "=":
        //            tableOperator = QueryComparisons.Equal;
        //            break;
        //        case "GT":
        //            tableOperator = QueryComparisons.GreaterThan;
        //            break;
        //        case "GE":
        //            tableOperator = QueryComparisons.GreaterThanOrEqual;
        //            break;
        //    }

        //    string partitionKeyVal = Convert.ToString(key);
        //    //TODO: always one parameter
        //    //object[] dbParams = new object[] { selectedDate.ToString(DateTimeFormat.SHORT_DATE_FORMAT) };
        //    string rowKeyVal = Convert.ToString(values[0]);

        //    TableQuery<CustomerEntity> rangeQuery = new TableQuery<CustomerEntity>().Where(
        //            TableQuery.CombineFilters(
        //                           TableQuery.GenerateFilterCondition("PartitionKey", 
        //                                QueryComparisons.Equal, partitionKeyVal),
        //                           TableOperators.And,
        //                           TableQuery.GenerateFilterCondition("RowKey", 
        //                                tableOperator, rowKeyVal)));

        //    foreach (CustomerEntity entity in table.ExecuteQuery(rangeQuery))
        //    {
        //        yield entity;
        //    }

        //    T cloudType=ConvertValue<CustomerEntity, TableEntity>
        //}

        public void Save(List<RowEntity> rowList)
        {
            if (rowList != null && rowList.Count > 0)
            {
                foreach (RowEntity entity in rowList)
                {
                    Save(entity);
                }
            }
        }

        public void Save(RowEntity entity)
        {
            string tableName=entity.GetTableName();
            Dictionary<string, object> values=entity.GetValues();
            Dictionary<string, DbType> attMetaData = entity.GetMetadata();

            var entityDict = new DictionaryTableEntity();
            foreach (string attName in values.Keys)
            {
                switch (attName)
                {
                    case TableInfo.HashKeyName:
                        entityDict.PartitionKey = Convert.ToString(values[attName]);
                        break;
                    case TableInfo.RangeKeyName:
                        entityDict.RowKey = Convert.ToString(values[attName]);
                        break;
                    default:
                        entityDict.Add(attName, values[attName], attMetaData[attName]);
                        break;
                }
            }

            var batch = new TableBatchOperation();
            batch.Add(TableOperation.InsertOrReplace(entityDict));
            CloudTable table = tableClient.GetTableReference(tableName);
            table.ExecuteBatch(batch);
        }



        public object Put(object item)
        {
            throw new NotImplementedException();
        }

        public object ExecuteScan(string tableName, string rangeName, string rangeValue, string oper, string lastHashValue, string lastRangeValue)
        {
            throw new NotImplementedException();
        }

        public object ExecuteQuery(string tableName, string hashValue, string lastHashValue, string lastRangeValue, int recordCount)
        {
            throw new NotImplementedException();
        }

        public object ExecuteQuery(string tableName, string hashValue, string hashRange, string rangeOperator, string lastHashValue, string lastRangeValue, int recordCount)
        {
            throw new NotImplementedException();
        }

        public object ExecuteQuery(string tableName, int hashValue, int hashRange, string rangeOperator, string lastHashValue, string lastRangeValue, int recordCount)
        {
            throw new NotImplementedException();
        }
    }
}
